package com.hand.wfive.util;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.hand.wfive.App;
import com.doris.media.picker.utils.MediaUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import kotlin.text.Charsets;

public class AssetsUtils {

    private static final String TAG = AssetsUtils.class.getSimpleName();

    /**
     * 从Assets中读取图片
     */
    public static Bitmap getImage(String fileName) {
        Bitmap image = null;
        try {
            InputStream is = App.getContext().getResources().getAssets().open(fileName);
            image = BitmapFactory.decodeStream(is);
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return image;

    }

    /**
     * 保存Assets图片到手机
     */
    public static void saveAssetsImg2Local(String filePath, String fileName) {
        AssetManager manager = App.getContext().getAssets();
        InputStream is;
        FileOutputStream fos = null;
        try {
            //采用边读边写的方式
            is = manager.open(filePath);
            File f = new File(App.getContext().getImgPath() + "/" + fileName);
            if (f.exists()) {
                logD("saveAssetsImg2Local(): exists -> delete " + f.delete());
            }
            fos = new FileOutputStream(f);
            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }
            MediaUtils.refreshSystemMedia(App.getContext(), f.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 把Assets文件复制到手机
     */
    public static void copyFile(String filePath, String copyPath) {
        try {
            InputStream is = App.getContext().getAssets().open(filePath);
            FileOutputStream fos = new FileOutputStream(copyPath);
            byte[] buffer = new byte[1024];
            int byteCount;
            while ((byteCount = is.read(buffer)) != -1) {
                fos.write(buffer, 0, byteCount);
            }
            fos.flush();
            is.close();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 从Assets中读取文件内容
     */
    public static ArrayList<String> getArrayText(String fileName) {
        ArrayList<String> text = new ArrayList<>();
        try {
            InputStream is = App.getContext().getResources().getAssets().open(fileName);
            InputStreamReader inputReader = new InputStreamReader(is);
            BufferedReader bufReader = new BufferedReader(inputReader);
            String line;
            while ((line = bufReader.readLine()) != null) {
                text.add(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return text;
    }

    /**
     * 从Assets中读取文件内容
     */
    public static String getText(String fileName) {
        String text = "";
        try {
            InputStream is = App.getContext().getResources().getAssets().open(fileName);
            byte[] buffer = new byte[is.available()];
            logD("getText(): read " + is.read(buffer));
            is.close();
            text = new String(buffer, Charsets.UTF_8);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return text;
    }

    private static void logD(String msg) {
        Log.d(TAG, msg);
    }
}
